# ---------- ---------- ---------- ---------- ---------- ----------
#
# op(eration)s Makefile
#
#    1) config, vars, and target
#    2) functions
#    3) phonies
#
# ---------- ---------- ---------- ---------- ---------- ----------


TMP_HOME=/tmp/ops/home
TMP_BACKUP=/tmp/ops/backup
HOME_DIR=$(if $(h),$(h),$(TMP_HOME))
BACKUP_DIR=$(if $(b),$(b),$(TMP_BACKUP))


target: help


# ---------- ---------- ---------- ---------- ---------- ----------
# functions
# ---------- ---------- ---------- ---------- ---------- ----------

define dry_run_sync_files
	@. ./src/backup.sh && sync_files $(1)/$(3) $(2) dry_run
endef


define sync_files
	@. ./src/backup.sh && sync_files $(1)/$(3) $(2)
endef


define do_func
	@$(call $(1),$(2),$(3),.gnupg)
	@$(call $(1),$(2),$(3),.irssi)
	@$(call $(1),$(2),$(3),.mutt)
	@$(call $(1),$(2),$(3),.password-store)
	@$(call $(1),$(2),$(3),.ssh)
	@$(call $(1),$(2),$(3),Documents)

	@$(call $(1),$(2),$(3),.bash_aliases_pvt)
	@$(call $(1),$(2),$(3),.mbsyncrc)
endef


# ---------- ---------- ---------- ---------- ---------- ----------
# phonies
# ---------- ---------- ---------- ---------- ---------- ----------

.PHONY: help
help:
	@echo "Usage: make [PHONY] [OPTIONS]"
	@echo "PHONY:"
	@sed -n -e "/sed/! s/\.PHONY: /    /p" Makefile
	@echo "OPTIONS:"
	@echo "    h"
	@echo "        Set the home directory location."
	@echo "    b"
	@echo "        Set the backup directory location."
	@. ./src/packages.sh && check_additional_packages


.PHONY: dry-run-backup
dry-run-backup:
	@$(call do_func,dry_run_sync_files,$(HOME_DIR),$(BACKUP_DIR))


.PHONY: dry-run-restore
dry-run-restore:
	@$(call do_func,dry_run_sync_files,$(BACKUP_DIR),$(HOME_DIR))


.PHONY: backup
backup:
	@$(call do_func,sync_files,$(HOME_DIR),$(BACKUP_DIR))


.PHONY: backup-to-tmp
backup-to-tmp:
	@mkdir -p $(TMP_HOME) $(TMP_BACKUP)
	@$(call do_func,sync_files,~,$(TMP_HOME))
	@$(call do_func,sync_files,$(TMP_HOME),$(TMP_BACKUP))
	@diff -r $(TMP_HOME) $(TMP_BACKUP)


.PHONY: configure
configure:
	@. ./src/manage_keys.sh && import_gnupg && set_permissions


.PHONY: install
install: install-min
	@. ./src/packages.sh && install_apt_packages_mailco
	@. ./src/packages.sh && install_apt_packages_gui


.PHONY: install-min
install-min:
	@. ./src/packages.sh && install_apt_packages


.PHONY: lint
lint:
	@shellcheck -s bash ./src/*


.PHONY: restore
restore:
	@$(call do_func,sync_files,$(BACKUP_DIR),$(HOME_DIR))
